package org.easy.elasticsearch;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.search.TotalHits;
import org.easy.elasticsearch.config.client.ElasticSearchClient;
import org.easy.elasticsearch.config.pool.ElasticSearchPool;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 
 * @Title: ElasticSearchApplication.java
 * @Package org.easy.elasticsearch
 * @Description: TODO
 * @author linNaibin
 * @date 2020年9月11日 下午3:16:56
 * @version V1.0
 */

@SpringBootApplication
public class ElasticSearchApplication {

	static void search(ElasticSearchClient client) {
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.from(0);
		sourceBuilder.size(10);
		sourceBuilder.fetchSource(new String[] { "title" }, new String[] {});
		// 对输入的 value 进行分词
		MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "标题");
		// 对输入的 value 不进行分词
		TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("tag", "体育");
		RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishTime");
		rangeQueryBuilder.gte("2018-01-26T08:00:00Z");
		rangeQueryBuilder.lte("2018-01-26T20:00:00Z");
		BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
		boolBuilder.must(matchQueryBuilder);
	//	boolBuilder.must(termQueryBuilder);
	//	boolBuilder.must(rangeQueryBuilder);
	//	sourceBuilder.query(boolBuilder);

		SearchRequest searchRequest = new SearchRequest("easy");
		// searchRequest.types("easy");
		searchRequest.source(sourceBuilder);
		try {
			System.out.println("client:::::" + client);
			RestHighLevelClient hc = client.getRestHighLevelClient();

			RequestOptions options = RequestOptions.DEFAULT;

			SearchResponse response = hc.search(searchRequest, options);

			System.out.println(response.getHits());

			SearchHits hits = response.getHits();
			TotalHits totalHits = hits.getTotalHits();
			System.out.println(totalHits.value);

			System.out.println(totalHits.toString());

			hits.forEach((hit) -> {

				System.out.println(hit.getId());
			});

			// SearchResponse response =
			// client.getRestHighLevelClient().getLowLevelClient().search(searchRequest);
			// System.out.println(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		System.out.println("Hello World!");
		ConfigurableApplicationContext context = SpringApplication.run(ElasticSearchApplication.class);

		ElasticSearchPool pool = context.getBean(ElasticSearchPool.class);

		ElasticSearchClient client = pool.getClient();

		try {
			// insert(client);
			search(client);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	static void insert(ElasticSearchClient client) throws IOException {

		RestHighLevelClient c = client.getRestHighLevelClient();
		IndexRequest indexRequest = new IndexRequest();

		indexRequest.index("easy"); // 索引名称
		// indexRequest.opType("test"); // 类型
		indexRequest.id("id0099"); // ID
		// ==============================提供文档源========================================
		Map<String, Object> source = new HashMap<String, Object>();
		source.put("title", "标题");
		source.put("date", new Date());

		indexRequest.source(source);

		RequestOptions options = RequestOptions.DEFAULT;

		c.index(indexRequest, options);

	}

}
